package com.iss.airsys.controller;

import com.iss.airsys.config.PrereadConfig;
import com.iss.airsys.entity.Employee;
import com.iss.airsys.model.MyResponseData;
import com.iss.airsys.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import static java.awt.SystemColor.info;

@RestController
@RequestMapping("employee/")
public class EmployeeController {
    @Autowired
    EmployeeService service;
    @Autowired
    private PrereadConfig config;
    @RequestMapping("selectAll")
    public List<Employee> selctAll(){
        return service.selctAll();
    }

    @RequestMapping("getId")
    public MyResponseData getId(HttpSession session){
        MyResponseData responseData = new MyResponseData();
        Integer em_id = (Integer)session.getAttribute("em_id");
        responseData.setErrorCode(em_id);
        System.out.println("getId==="+em_id);
        return responseData;
    }

    //员工信息的获取
    @RequestMapping("setEmpMessage")
    public MyResponseData setEmpMassage(Employee employee,HttpSession session){
        MyResponseData responseData = new MyResponseData();
        System.out.println("employee====="+employee);
       session.setAttribute("em_name",employee.getEm_name());
       session.setAttribute("em_idcard",employee.getEm_idcard());
       session.setAttribute("em_address",employee.getEm_address());
       session.setAttribute("em_phone",employee.getEm_phone());
       session.setAttribute("em_enjobtime",employee.getEm_enjobtime());

       return responseData;
    }
    @RequestMapping("getEmpMassage")
    public MyResponseData getEmpMassage(HttpSession session){
        MyResponseData responseData = new MyResponseData();
        List<Object> list = new ArrayList<>();
        String em_name = (String) session.getAttribute("em_name");
        String em_idcard =(String) session.getAttribute("em_idcard");
        String em_address = (String)session.getAttribute("em_address");
        String em_phone = (String)session.getAttribute("em_phone");
        String em_enjobtime =(String) session.getAttribute("em_enjobtime");
        Employee employee = new Employee();
        employee.setEm_name(em_name);
        employee.setEm_idcard(em_idcard);
        employee.setEm_address(em_address);
        employee.setEm_phone(em_phone);
        employee.setEm_enjobtime(em_enjobtime);
        responseData.setObject(employee);
        return responseData;
    }

    //查询员工图片
    @RequestMapping("selectpicture")
    public MyResponseData selectpicture(Integer  em_id){
        System.out.println("em_id="+em_id);
        MyResponseData responseData = new MyResponseData();
        String em_picture= service.selectpicture(em_id);
        System.out.println("em_picture="+em_picture);
        responseData.setStatus(em_picture);
        return responseData;
    }


    //上传下载
    @RequestMapping("upload")
    public  MyResponseData upload(@RequestParam(value = "files", required = false) MultipartFile[] files, Integer em_id, HttpServletRequest request){
        MyResponseData responseData = new MyResponseData();
        System.out.println(em_id);
        System.out.println(files);
        if (files == null || files.length == 0) {
            responseData.setErrorCode(-1);
            responseData.setStatus("没有选择文件");
        }

        File path = new File(config.getUploadPath()+File.separator+em_id);
        if (!path.exists()) {
            path.mkdirs();
        }

        String surfaceName = "";
        for (int i = 0; i < files.length; i++) {
            String newFileName = System.currentTimeMillis() + "_" + files[i].getOriginalFilename();
            File newFile = new File(path, newFileName);   //保存文件
            if (i == 0) {
                surfaceName = "uploadFile"+File.separator+em_id+File.separator+newFileName;
            }
            try {
                FileOutputStream fos = new FileOutputStream(newFile);
                InputStream is = files[i].getInputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                is.close();
                responseData.setErrorCode(0);
                responseData.setStatus("上传成功");
                Integer count = service.updateInfo(surfaceName,em_id);
                responseData.setStatus(surfaceName);
            } catch (IOException e) {
                responseData.setErrorCode(-2);
                responseData.setStatus("上传失败");
                e.printStackTrace();
            }
              }
        return responseData;
    }
}
